Python “from . import x” 您所在的位置:网站首页 python to_period Python “from . import x”

Python “from . import x”

#Python “from . import x” | 来源: 网络整理| 查看: 265

5/5 - (1 vote)

You may have seen an import statement from . import your_module with the dot after the from keyword:

from . import your_module What Does the Dot Mean?

In Python, modules are defined in packages. If you want to import a certain module within a package that may be hierarchically structured, you need to specify the path of your module.

└── project | your_module.py ├── your_package_1 │ ├── your_module_1.py │ └── your_module_2.py └── your_package_2 │ ├── your_module_1.py │ └── your_module_2.py

The dot (.) symbol after in an import statement of the form from . import your_module is a Python syntactical element for relative imports. It means “look for the module in your current folder”. The current folder is the one where the code file resides from which you run this import statement.

The concept of absolute and relative imports was introduced in PEP 328 to differentiate from which package a module should be imported.

An absolute import searches the module in your top-level package or project.A relative import searches the module from the folder where the current Python file executing the import statement resides.

When using relative imports you can use more than one dot to refer to the parent folders within your packages. For example, two dots in from .. import your_module would import the module from the parent folder, and three dots in from ... import your_module would import the module from the parent of the parent.

from .. import your_module # searches "your_module.py" in the parent folder from ... import your_module # searches "your_module.py" in the grandparent folder

You can see this example in the following graphic in a sample PyCharm project:

You add two import statements to your main.py file with relative imports. The first imports from the current sub-package. The second imports from the parent package. Even though both imported modules have the same names, they can be differentiated through the use of relative (and absolute paths).

By the way, if you want to boost your PyCharm skills—one of the highly-leveraged activities you can do as a programmer—check out our Finxter Academy course that offers you a full-fledged mastery introduction into the ins and outs of PyCharm.

*** Join Course: Mastering the PyCharm IDE for Maximum Python Productivity ***

Chris

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有